#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
#=============================================================================
#     FileName: app.py
#         Desc: main application
#      License: GPL
#       Author: Steve Lemuel
#        Email: wlemuel@hotmail.com
#      Version: 0.0.1
#   LastChange: 2015-09-07 13:45:01
#    CreatedAt: 2015-09-07 13:45:01
#=============================================================================
"""
import os
import logging
import time
logging.basicConfig()
from pytz import utc
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.blocking import BlockingScheduler

from newsparser import NewsParser, TumblrParser
from job import Job, ConfirmJob, TumblrParseJob, TumblrDownloadJob

# init
news_parser = NewsParser()


def job_function():
    print time.strftime('[%Y-%m-%d %H:%M:%S] '),
    print 'Job starting ...'
    Job(news_parser).run()
    print time.strftime('[%Y-%m-%d %H:%M:%S] '),
    print 'Job finished.'


def confirmJob_function():
    print time.strftime('[%Y-%m-%d %H:%M:%S] '),
    print 'Confirm job starting ...'
    ConfirmJob().run()
    print time.strftime('[%Y-%m-%d %H:%M:%S] '),
    print 'Confirm job finished.'


def tumbler_parse_job():
    print time.strftime('[%Y-%m-%d %H:%M:%S] '),
    print 'Confirm tumbler parse job starting ...'
    TumblrParseJob(TumblrParser()).run()
    print time.strftime('[%Y-%m-%d %H:%M:%S] '),
    print 'Confirm tumbler parse job finished.'


def tumbler_download_job():
    print time.strftime('[%Y-%m-%d %H:%M:%S] '),
    print 'Confirm tumbler download job starting ...'
    TumblrDownloadJob().run()
    print time.strftime('[%Y-%m-%d %H:%M:%S] '),
    print 'Confirm tumbler download job finished.'


if __name__ == '__main__':
    # Start Scheduler
    sched = BlockingScheduler()

    # add job
    sched.add_job(job_function, 'interval', hours=2, id='fetch_news')
    sched.add_job(
        confirmJob_function, 'interval', minutes=45, id='confirm_news')

    #sched.add_job(
    #tumbler_parse_job, 'interval', minutes=20, id="tumbler_parse")
    # sched.add_job(
    #     tumbler_download_job, 'interval', minutes=30, id="tumbler_download")

    print time.strftime('[%Y-%m-%d %H:%M:%S] pynhk started')
    sched.start()
